from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains  # 鼠标拖动

driver = webdriver.Chrome(executable_path="../chromedriver.exe")

url = "https://mail.qq.com/"
driver.get(url)
time.sleep(3)
driver.switch_to.frame("login_frame")
driver.find_element_by_id("u").send_keys("1428515626")
time.sleep(0.5)
driver.find_element_by_id("p").send_keys("cdsfds2121")
time.sleep(1)
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, "login_button")))  # 判断按钮是否可以点击
driver.find_element_by_id("login_button").click()
time.sleep(2)
driver.switch_to.frame("tcaptcha_iframe")  # 滑块又是一个新的frame框架 需要重新更改
time.sleep(0.4)
while True:
    slider = WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "tcaptcha_drag_thumb")))
    distance = 190
    actions = webdriver.ActionChains(driver)
    # 点击开始拖拽
    actions.click_and_hold(slider)  # 先一直点击不动
    actions.pause(0.3)  # 暂停0.2秒
    actions.move_by_offset(distance + 20, 0)
    actions.pause(0.15)
    actions.move_by_offset(-35, 0)
    actions.pause(0.6)
    actions.release()  # 松开按钮
    actions.perform()
    time.sleep(2)
    try:
        shuaxin = WebDriverWait(driver, 1).until(ec.presence_of_element_located((By.ID, "e_reload")))
        driver.find_element_by_id("e_reload").click()
        time.sleep(1)
    except:
        driver.quit()
        break
